package com.hydra.baseui.textview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.TextView;

public class SpannableTextView extends TextView implements OnClickListener, OnLongClickListener {
	
	private OnClickListener mOnClickListener;
	private OnLongClickListener mOnLongClickListener;
	
	private boolean mReceiveClick;
	private boolean mHasPerformLongClick;
	
	public SpannableTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		init();
	}

	public SpannableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		init();
	}

	public SpannableTextView(Context context) {
		super(context);
		
		init();
	}
	
	private void init() {
		mReceiveClick = true;
		mHasPerformLongClick = false;
		
		setMovementMethod(GLinkMovementMethod.getInstance());
		
		super.setOnClickListener(this);
		super.setOnLongClickListener(this);
	}
	
	public void setCanReceiveClick(boolean canReceiveClick) {
		mReceiveClick = canReceiveClick;
	}
	
	public void resetLongClickFlag() {
		mHasPerformLongClick = false;
	}
	
	public boolean hasPerformLongClick() {
		return mHasPerformLongClick;
	}
	
	@Override
	public void setOnClickListener(OnClickListener l) {
		mOnClickListener = l;
	}
	
	@Override
	public void setOnLongClickListener(OnLongClickListener l) {
		mOnLongClickListener = l;
	}
	
	@Override
	public void onClick(View v) {
		if(mReceiveClick && mOnClickListener != null) {
			mOnClickListener.onClick(v);
		}
	}

	@Override
	public boolean onLongClick(View v) {
		mHasPerformLongClick = true;
		
		return mOnLongClickListener != null && mOnLongClickListener.onLongClick(v);
	}
}
